package rs.udd.service.mappings;

import rs.udd.model.Advertiser;
import rs.udd.service.dto.advertiser.AdvertiserEditDto;
import rs.udd.service.dto.advertiser.AdvertiserRegisterDto;
import rs.udd.service.dto.advertiser.AdvertiserViewDto;
import rs.udd.service.dto.user.UserPasswordEditDto;

public class AdvertiserMapping {

	// this private constructor prevents this class from instantiation
	private AdvertiserMapping() {
	}

	public static Advertiser register(final AdvertiserRegisterDto dto) {

		Advertiser advertiser = new Advertiser();

		advertiser.setUsername(dto.getUsername());
		advertiser.setPassword(dto.getPassword());
		advertiser.setAdvertiserType(dto.getAdvertiserType());
		advertiser.setFirstname(dto.getFirstname());
		advertiser.setLastname(dto.getLastname());
		advertiser.setEmail(dto.getEmail());
		advertiser.setAddress(dto.getAddress());
		advertiser.setPhoneNumber(dto.getPhoneNumber());
		
		advertiser.setEmailActive(false); // when registered, his email is considered as inactive.
		advertiser.setActive(false); // newly registered advertiser must first activate it's account via email confirmation

		return advertiser;
	}
	

	public static AdvertiserEditDto edit(final Advertiser advertiser) {

		AdvertiserEditDto dto = new AdvertiserEditDto();
		// TODO

		return dto;
	}

	public static void edit(final AdvertiserEditDto dto, Advertiser advertiser) {

		// TODO
		advertiser.setFirstname(dto.getFirstname());
		advertiser.setLastname(dto.getLastname());
		advertiser.setAdvertiserType(dto.getAdvertiserType());
		advertiser.setAddress(dto.getAddress());
		advertiser.setPhoneNumber(dto.getPhoneNumber());
		
		String oldEmail = advertiser.getEmail();
		String newEmail = dto.getEmail();
		if (!oldEmail.equals(newEmail)) {
			advertiser.setEmailActive(false); // when advertiser confirms his changed email, it will be set as active
		}
	}

	public static void editPassword(final UserPasswordEditDto dto, Advertiser advertiser) {

		advertiser.setPassword(dto.getNewPassword());
	}
	
	public static UserPasswordEditDto editPassword(Advertiser advertiser) {
		
		UserPasswordEditDto dto = new UserPasswordEditDto();
		
		dto.setId(advertiser.getId());
		
		return dto;
	}

	public static AdvertiserViewDto view(final Advertiser advertiser) {

		AdvertiserViewDto dto = new AdvertiserViewDto();
		
		dto.setId(advertiser.getId());
		dto.setUsername(advertiser.getUsername());
		dto.setPassword(advertiser.getPassword());
		dto.setAdvertiserType(advertiser.getAdvertiserType());
		dto.setFirstname(advertiser.getFirstname());
		dto.setLastname(advertiser.getLastname());
		dto.setEmail(advertiser.getEmail());
		dto.setAddress(advertiser.getAddress());
		dto.setPhoneNumber(advertiser.getPhoneNumber());
		dto.setCreationTime(advertiser.getCreationTime());

		return dto;
	}
	
	public static AdvertiserViewDto viewToEdit(final Advertiser advertiser) {
		AdvertiserViewDto dto = new AdvertiserViewDto();
		
		dto.setId(advertiser.getId());
		dto.setAdvertiserType(advertiser.getAdvertiserType());
		dto.setFirstname(advertiser.getFirstname());
		dto.setLastname(advertiser.getLastname());
		dto.setEmail(advertiser.getEmail());
		dto.setAddress(advertiser.getAddress());
		dto.setPhoneNumber(advertiser.getPhoneNumber());

		return dto;
	}

}
